package com.nexgo.payment.trans.setting.othersfunc;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 签购单打印参数设置
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月15日
 */
public class SetupPrintParamActivity extends SaveParameterActivity implements AdapterView.OnItemSelectedListener {
    private static final String TAG = SetupPrintParamActivity.class.getSimpleName();

    @BindView(R.id.check_print_sales_slip)
    SwitchButton checkPrintSalesSlip;
    @BindView(R.id.et_print_title)
    EditText etPrintTitle;
    @BindView(R.id.spinner_print_title)
    Spinner spinnerPrintTitle;
    @BindView(R.id.et_service_hot_line)
    EditText etServiceHotLine;
    @BindView(R.id.et_bank_name)
    EditText etBankName;

    private String mIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_print_param);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.order_print))
                .build();
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.IS_PRINT_SALES_SLIP, checkPrintSalesSlip.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRINT_TITLE, etPrintTitle.getText().toString());
        map.put(PreferencesConstants.SERVICE_PHONE, etServiceHotLine.getText().toString());
        map.put(PreferencesConstants.UNKNOWN_ISSUING_BANK_NAME, etBankName.getText().toString());
        map.put(PreferencesConstants.PRINT_TITLE_SELECT, mIndex);
        return map;
    }

    @Override
    public void showDetail() {
        //绑定监听事件
        spinnerPrintTitle.setOnItemSelectedListener(this);

        checkPrintSalesSlip.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.IS_PRINT_SALES_SLIP)));
        etPrintTitle.setText(mPresenter.getParamValue(PreferencesConstants.PRINT_TITLE));
        etServiceHotLine.setText(mPresenter.getParamValue(PreferencesConstants.SERVICE_PHONE));
        etBankName.setText(mPresenter.getParamValue(PreferencesConstants.UNKNOWN_ISSUING_BANK_NAME));
        //初始打印抬头类型
        mIndex = mPresenter.getParamValue(PreferencesConstants.PRINT_TITLE_SELECT);
        spinnerPrintTitle.setSelection(Integer.parseInt(StringUtils.firstNonEmpty(mIndex, "0")));
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mIndex = String.valueOf(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
